/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import cn.hutool.core.util.StrUtil;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.PrePurchaseRequest;
import me.zhengjie.modules.system.domain.vo.PrePurchaseRequestAuditVo;
import me.zhengjie.modules.system.service.PrePurchaseRequestService;
import me.zhengjie.modules.system.service.dto.PrePurchaseRequestQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.modules.system.service.dto.PrePurchaseRequestDto;
import org.springframework.web.multipart.MultipartFile;

/**
* @website https://eladmin.vip
* @author 李忠臻
* @date 2024-03-08
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "请购单管理")
@RequestMapping("/api/prePurchaseRequest")
public class PrePurchaseRequestController {

    private final PrePurchaseRequestService prePurchaseRequestService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('prePurchaseRequest:list')")
    public void exportPrePurchaseRequest(HttpServletResponse response, PrePurchaseRequestQueryCriteria criteria) throws IOException {
        prePurchaseRequestService.download(prePurchaseRequestService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询请购单")
    @ApiOperation("查询请购单")
//    @PreAuthorize("@el.check('prePurchaseRequest:list')")
    public ResponseEntity<PageResult<PrePurchaseRequestDto>> queryPrePurchaseRequest(PrePurchaseRequestQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(prePurchaseRequestService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增请购单")
    @ApiOperation("新增请购单")
    @PreAuthorize("@el.check('prePurchaseRequest:add')")
    public ResponseEntity<Object> createPrePurchaseRequest(@Validated @RequestBody PrePurchaseRequest resources){
        prePurchaseRequestService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改请购单")
    @ApiOperation("修改请购单")
    @PreAuthorize("@el.check('prePurchaseRequest:edit')")
    public ResponseEntity<Object> updatePrePurchaseRequest(@Validated @RequestBody PrePurchaseRequest resources){
        prePurchaseRequestService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除请购单")
    @ApiOperation("删除请购单")
    @PreAuthorize("@el.check('prePurchaseRequest:del')")
    public ResponseEntity<Object> deletePrePurchaseRequest(@RequestBody Long[] ids) {
        prePurchaseRequestService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/pass")
    @Log("审核通过请购单申请主表")
    @ApiOperation("审核通过请购单申请主表")
    @PreAuthorize("@el.check('prePurchaseRequest:audit')")
    public ResponseEntity<Object> passPrePurchaseRequest(@RequestBody PrePurchaseRequestAuditVo prePurchaseRequestAuditVo) {
        prePurchaseRequestService.pass(prePurchaseRequestAuditVo.getIds(), prePurchaseRequestAuditVo.getB2b());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/cancel/{id}")
    @Log("审核不通过请购单申请主表")
    @ApiOperation("审核不通过请购单申请主表")
    @PreAuthorize("@el.check('prePurchaseRequest:audit')")
    public ResponseEntity<Object> cancelPrePurchaseRequest(@PathVariable Long id) {
        prePurchaseRequestService.cancel(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/upload")
    @ApiOperation("上传文件")
    @PreAuthorize("@el.check('prePurchaseRequest:add')")
    public ResponseEntity<Object> uploadPurchaseRequest(@RequestParam String supplierName,
                                                        @RequestParam String purchaseAttribute,
                                                        @RequestParam("file") MultipartFile file){
//        localStorageService.create(name, file);
//        PurchaseRequest resources = new PurchaseRequest();
        if ("null".equals(supplierName) || StrUtil.isEmpty(supplierName)) {
            throw new BadRequestException("供应商名称必须填写！");
        }
        if ("null".equals(purchaseAttribute) || StrUtil.isEmpty(purchaseAttribute)) {
            throw new BadRequestException("采购性质称必须填写！");
        }

        prePurchaseRequestService.upload(supplierName, purchaseAttribute, file);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }
}